/*
 * Author: 李波(Edwin Lee)
 * Date: 2024-07-07 23:30:18
 * FilePath: /code/C++/auto/main.cpp
 * LastEditTime: 2024-07-31 16:27:01
 * version: 1.0
 * Description:编译要添加-std=c++17 
 */
#include <iostream>
#include <cxxabi.h>
using namespace std;

//测试auto自动推到
void test1()
{
	cout << "----------test1----------" << endl;
	auto x = 3.5 + 15l;
	// double
	cout << abi::__cxa_demangle(typeid(x).name(),NULL,NULL,NULL) << endl;
}

//测试auto自动推导的类型退化问题
void test2()
{
	cout << "----------test2----------" << endl;
	const int a = 10;
	auto x = a;//const int放在右值时会退化成int
	cout << abi::__cxa_demangle(typeid(x).name(),NULL,NULL,NULL) << endl;

	int b = 20;
	int& c = b;
	auto y = c;//引用类型放在右值会退化为非引用类型
	cout << abi::__cxa_demangle(typeid(y).name(),NULL,NULL,NULL) << endl;
	cout << abi::__cxa_demangle(typeid(c).name(),NULL,NULL,NULL) << endl;

	// 数组类型会退化为指针类型
	int d[3] = { 1,2,3 };
	auto z = d;
	// int [3]
	cout << abi::__cxa_demangle(typeid(d).name(),NULL,NULL,NULL) << endl;
	// int*
	cout << abi::__cxa_demangle(typeid(z).name(),NULL,NULL,NULL) << endl;
}

//auto& 推导出引用类型，可以避免类型退化
void test3()
{
	cout << "----------test3----------" << endl;
	const int a = 10;
	const auto x = a;//const int放在右值时会退化成int
	auto& info = typeid(decltype(a));
	//这里结果是int,而不是const int说明typeid的结果不含const
	cout << abi::__cxa_demangle(typeid(decltype(a)).name(),NULL,NULL,NULL) << endl;
	cout << abi::__cxa_demangle(typeid(decltype(x)).name(),NULL,NULL,NULL) << endl;
	cout << is_same_v<decltype(x),const int> << endl;//int&
}

int main()
{
	test1();
	test2();
	test3();
	return 0;
}
